package io.shallwe.common.controller;

import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;

import io.shallwe.common.model.Account;
import io.shallwe.login.LoginService;

public class BaseController extends Controller{
	private Account loginAccount = null;
	
	protected void _clear_() {
		//this.loginAccount = null;
		super._clear_();
	}
	
	@NotAction
	public Account getLoginAccount() {
		if (loginAccount == null) {
			loginAccount = getAttr(LoginService.loginAccountCacheName);
			if (loginAccount != null && ! loginAccount.isStatusOk()) {
				throw new IllegalStateException("当前用户状态不允许登录，status = " + loginAccount.getStatus());
			}
		}
		return loginAccount;
	}
	
	@NotAction
	public String getLoginAccountId() {
		return getLoginAccount().getId();
	}
	
	
	@NotAction
	public boolean isLogin() {
		return getLoginAccount() != null;
	}

	@NotAction
	public boolean notLogin() {
		return !isLogin();
	}
	
	
	@NotAction
	public boolean isPjaxRequest() {
		return "true".equalsIgnoreCase(getHeader("X-PJAX"));
	}

	@NotAction
	public boolean isAjaxRequest() {
		return "XMLHttpRequest".equalsIgnoreCase(getHeader("X-Requested-With"));
	}
	
	
}
